#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash VTK-7.1.1.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then
    echo "请输入正确的构建路径"
    exit 1
fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then
    echo "请输入正确的安装路径"
    exit 1
fi

buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y wget tar cmake environment-modules
wget -t 10 -c -P $buildpath https://www.vtk.org/files/release/7.1/VTK-7.1.1.tar.gz

tar xf $buildpath/VTK-7.1.1.tar.gz -C $buildpath && rm -rf $buildpath/VTK-7.1.1.tar.gz
cd $buildpath/VTK-7.1.1
mkdir build
cd build
cmake .. -D CMAKE_INSTALL_PREFIX=$installpath/VTK -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
make -j$(nproc)
make install

cat >"$installpath/VTK/vtk_modulefiles" <<EOF
#%Module1.0
conflict vtk
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
        set modfile_path [file dirname \$modfile]
            return \$modfile_path
    }
set pwd [getModulefileDir]
set VTK \$pwd
setenv VTK \$VTK
prepend-path VTK_DIR \$VTK
EOF
rm -rf $buildpath/VTK-7.1.1
